<include file="Public/header"/>
<include file="Public/menu"/>
<style type="text/css">
.edit-batch-list .ebatch-name{
    width:200px;
}
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            促销
            <small>组合促销</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Index/index')}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{:U('group')}"><i class="fa fa-dashboard"></i> 组合促销</a></li>
            <li class="active">编辑</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">编辑组合套餐</h3>
                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="{:U('group')}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp;列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="form" action="{:U('Promotion/groupEdit')}" method="post" accept-charset="UTF-8" class="form-horizontal" onsubmit="return checkSubmit();">
                        <input type="hidden" name="id" value="{$info.id}">
                        <div class="box-body">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">组合套餐描述名称</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input name="name" value="{$info.name}" type="text" class="form-control" placeholder="输入组合套餐描述名称" <if condition="$info['activing']">readonly</if>>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">组合商品</label>
                                <if condition="!$info['activing']">
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <button type="button" class="btn btn-default pull-left" id="daterange-btn">
                                            <span>
                                              <i class="fa fa-calendar"></i> 点击添加商品
                                            </span>
                                            <i class="fa fa-caret-down"></i>
                                        </button>
                                    </div>
                                </div>
                                </if>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-6">
                                    <div class="box box-primary col-sm-6">
                                        <!-- /.box-header -->
                                        <div class="box-body">
                                            <ul class="todo-list ui-sortable edit-batch-list">
                                                <volist name="list" id="vo">
                                                    <li class="one-product">
                                                        <input type="hidden" name="sp_id[]" value="{$vo.sp_id}">
                                                        <input type="hidden" name="product_id[]" value="{$vo.product_id}">
                                                        <input type="hidden" name="item_id[]" value="{$vo.id}">
                                                        <a href="javascript:;" title="{$vo.name}" class="text ebatch-name">{$vo.name}</a>
                                                        <small class="label label-warning"> 售价 : {$vo.price}</small>
                                                        <small class="label label-success"> 库存 : {$vo.store}</small>
                                                        货品数量 : <span class="text shuliang"><input type="text" class="geshu" name="num[]" data-price="{$vo.price}" data-num="{$vo.store}" value="{$vo.num}" style="width:50px;" <if condition="$info['activing']">readonly</if> ></span>
                                                        <if condition="!$info['activing']">
                                                        <div class="tools">
                                                            <i class="fa fa-trash-o icondel" data-id="{$vo.sp_id}" onclick="delGoodsItem(this)"></i>
                                                        </div>
                                                        </if>
                                                    </li>
                                                </volist>
                                            </ul>
                                        </div>
                                        <input type="hidden" id="yc-goods-ids" name="tempids" value="{$ids_str}">
                                        <!-- /.box-body -->
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">优惠总价格</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input id="total_price" name="total_price" value="{$info.total_price}" type="text" class="form-control" placeholder="输入优惠总价格" <if condition="$info['activing']">readonly</if>/>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">开始时间</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <if condition = "(!empty($info))">
                                        	<input id="start_time" name="start_time" class="form-control" type="text" value="{$info.start_time|date='Y-m-d H:i',###}" <if condition="$info['activing']">disabled<else />readonly</if> />
                                        <else/>
                                        	<input id="start_time" name="start_time" class="form-control" type="text" value="" readonly />
                                        </if>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-2 control-label">结束时间</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <if condition = "(!empty($info))">
                                        	<input id="end_time" name="end_time" class="form-control" type="text" value="{$info.end_time|date='Y-m-d H:i',###}" <if condition="$info['activing']">disabled<else />readonly</if> />
                                        <else/>
                                        	<input id="end_time" name="end_time" class="form-control" type="text" value="" readonly />
                                        </if>                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <if condition="!$info['activing']">
                        <div class="box-footer">
                            <input type="hidden" name="_token" value="7lMdsZohP4DDH6f9Pvt6gn9e8TR3yOc34ovag8h3">
                            <div class="col-sm-2"></div>
                            <div class="col-sm-6">
                                <div class="btn-group pull-right">
                                    <button type="submit" class="btn btn-info pull-right">提交</button>
                                </div>
                            </div>
                        </div>
                        </if>
                        <!-- /.box-footer -->
                    </form>
                </div>

            </div>
        </div>

    </section>
</div>
<!-- 底部 -->

<include file="Public/common_footer"/>
<script src="/Public/layer/layer.js"></script>
<link rel="stylesheet" href="/Public/admin/jedate/jedate.css" />
<script src="/Public/admin/jedate/jedate.js"></script>
<script type="text/javascript">
    // 设置时间
    function setJedate(){
        //不显示秒的解决办法
        var stxt = $("#start_time").val().trim();
        if(stxt.length == 19){
            $("#start_time").val(stxt.substr(0, 16));
        }
        var etxt = $("#end_time").val().trim();
        if(etxt.length == 19){
            $("#end_time").val(etxt.substr(0, 16));
        }
        var start = {
            dateCell: '#start_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                end.minDate = datas; 
            }
        };
        var end = {
            dateCell: '#end_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                start.maxDate = datas;
            }
        };
        jeDate(start);
        jeDate(end);
    }
    setJedate();
    $('#daterange-btn').click(function () {
        layer.open({
            type: 2,
            title: '店铺商品库',
            area: ['1400px', '830px'],
            fixed: false,
            maxmin: true,
            content: "{:U('Promotion/getProducts','' , '')}"
        });

    });
	var ycTotalPrice = "0.00";
    var ychser = new YcChoser();
    ychser.setChosenIds($("#yc-goods-ids").val());
    function delGoodsItem(o) {
        ychser.isRepeat($(o).attr("data-id"));
        ychser.remove();
        $("#yc-goods-ids").val(ychser.getChosenIds());
        $(o).parents("li").remove();
    }
	function updateGoodsIds(ids) {
        $("#yc-goods-ids").val(ids);
        ychser.setChosenIds(ids);
    }
	
	var checkSubmitFlag = false;
	// 检查提交时候的商品数据
	function checkSubmit(){
		checkSubmitFlag = false;
		var o;
		var curVal;
		var tmpFlag = false;
		if(ychser.getLength() < 2){
 			GErr.show($("#yc-goods-ids"), "组合商品的数量不小于2");
		}else{
			GErr.remove($("#yc-goods-ids"));
			tmpFlag = true;
		}
		checkSubmitFlag = tmpFlag;
		tmpFlag = true;
		$(".one-product").each(function(){			
			var rs = "";
			var o;
			var curVal;
			var tmpFlag = false;
			var tmpVal;
			o = $(this).find(".geshu");
			tmpVal = parseInt(o.attr("data-num"));
			if(parseFloat(o.attr("data-price")) < 0.01){
				rs = "售价小于0.01，请移除掉该商品！";
			}else if(tmpVal <= 0){
				rs = "库存不足，请移除掉该商品！";
			}else if(!mbValidator.isPositiveInt(o.val())){
				tmpVal = tmpVal;
				rs += (rs == "" ? "" : "，") + "货品数量必须为大于0的整数";
			}else if(parseInt(o.val()) > tmpVal){
				rs += (rs == "" ? "" : "，") + "货品数量不能大于"+tmpVal;
			}
			if(rs == ""){
				$(this).find(".err-holder").remove();
			}else{
				checkSubmitFlag = false;
				$(this).find(".err-holder").remove();
				$(this).append('<div class="err-holder"><span class="error">'+rs+'</span></div>');
			}			
		});
		if(!tmpFlag) return;
		tmpFlag = false;
		ycTotalPrice = "0.00";
		$(".one-product").each(function(){
			var o = $(this).find(".geshu");
			tmpVal = getFixedFloat(o.attr("data-price"), o.val(), "*");
			ycTotalPrice = getFixedFloat(ycTotalPrice, tmpVal, "+");
		});
		o = $("#total_price");
		curVal = parseFloat(o.val());
		mbValidator.fixPrice(o, o.val());
		if(curVal < 0.01){
			GErr.show(o, "优惠总价格必须大于等于0.01");
		}else if(parseFloat(curVal) >= parseFloat(ycTotalPrice)){
			GErr.show(o, "优惠总价格不能大于"+ycTotalPrice);
		}else{
			GErr.remove(o);
			tmpFlag = true;
		}
		checkSubmitFlag = checkSubmitFlag && tmpFlag;
		return checkSubmitFlag;
	}    
    $("#form").validate({
        ignore: "",
        rules: {
            name:{
                required: true,
                rangelength:[2, 50]
            },
            start_time:{
                required: true,
                isActTime: true
            },
            end_time:{
                required: true,
                isActTime: true,
                dateTimeBigThan: "#start_time"
            }
        },
        messages:{
            name:{
                required: "请输入组合套餐描述名称"
            },
            start_time:{
                required: "请输入开始时间"
            },
            end_time:{
                required: "请输入结束时间",
                dateTimeBigThan: "【注意：】结束时间不能小于开始时间"
            }
        },
        submitHandler: function(form){
			if(checkSubmitFlag){
				form.submit();
			}
        }
    }); 
</script>
